package kltn.business;

import java.util.ArrayList;
import kltn.audioguidetravel.R;
import kltn.data.InfoTravel;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class InfoTravelAdapter extends ArrayAdapter<InfoTravel>{

	ArrayList<InfoTravel> infoTravels;
	Context context;
	int resource;
	boolean fag=false;
	public InfoTravelAdapter(Context context, int resource,
			ArrayList<InfoTravel> infoTravels) {
		super(context, resource,infoTravels);
		this.context=context;
		this.resource=resource;
		this.infoTravels=infoTravels;
		if(resource==R.layout.item_choose)
			fag=true;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View view= View.inflate(context, resource, null);
		TextView tvTitle= (TextView) view.findViewById(R.id.tvTitle);
		TextView tvDesc= (TextView) view.findViewById(R.id.tvDesc);
		TextView tvDistance= (TextView) view.findViewById(R.id.tvDistance);
		ImageView ivImg= (ImageView) view.findViewById(R.id.ivImg);
		String desc=infoTravels.get(position).getDescription();
		if(desc.length()>100){
			desc=desc.substring(0,100);
			int index=desc.lastIndexOf(" ");
			desc=desc.substring(0,index)+"...";
		}
		InfoTravel infoTravel= new InfoTravel(10.375206182710976,107.06889152526855);
		tvTitle.setText(infoTravels.get(position).getName());
		int distan=Helper.getDistance(infoTravels.get(position).getLatLng(),infoTravel.getLatLng());
		String strDistan=distan+"m";
		if(distan>=1000)
			strDistan=(Math.round(distan/1000))+"km";
		tvDistance.setText(strDistan);
		tvDesc.setText(desc);
		ivImg.setImageBitmap(infoTravels.get(position).getImg());
		if(fag){
			CheckBox cbChoose= (CheckBox) view.findViewById(R.id.cbInfo);
			cbChoose.setChecked(infoTravels.get(position).isShow());
			cbChoose.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					infoTravels.get(position).setShow(isChecked);
				}
			});
		}
		return view;
	}

}
